const express = require("express")
const router = express.Router();
const passport = require("passport")
const mongoose = require('mongoose')

// 引入 模型 
const Map = require("../../models/map");
const Floors = require("../../models/floors");
const Hole = require("../../models/hole");

// 新增楼层数据
router.post('/floor', (req, res) => {
  const pro = {};
  if (req.body.floor) pro.floor = req.body.floor;
  if (req.body.data) pro.data = req.body.data;
  new Floors(pro).save()
    .then(Floors => {
      res.json(Floors)
      console.log(Floors);
    })
})
// 查询楼层根据floor查找
router.get('/floor/:floor', async (req, res) => {
  console.log(req.params.floor)
  Floors.find({ floor: req.params.floor }).then(data => res.json(data))
})
// 查找所有楼层 
router.get('/floor', async (req, res) => {
  const data = await Floors.find()
  let datas = []
  await Promise.all(
    data.map(async (item) => {
      let obj = JSON.parse(JSON.stringify(item))
      const hole = await Hole.find({ floor_id: item._id })
      obj.children = hole
      datas.push(obj)
      console.log(new Date())
    }))
  console.log(`返回接口时间${new Date()}`)
  res.json(datas)

})
// 修改楼层数据
router.post('/floor/:id', async (req, res) => {
  console.log(`post参数${req.params.floor}`);
  const pro = {};
  if (req.body.title) pro.title = req.body.title;
  if (req.body.floor) pro.floor = req.body.floor;
  console.log(pro)
  Floors.findOneAndUpdate({ _id: req.params.id }, { $set: pro }, { new: true }).then(item => {
    console.log(item.hole)
    res.json(item)
  })
})


//GET api/map 获取数据
router.get('/', async (req, res) => {
  const data = await Floors.find()
  let arr = []
  data.forEach(item => {
    let obj = {
      base: "",
      hole: [],
      romm: []
    }
    if (item.children) {
      item.children.forEach(element => {
        if (element.category == 1) {
          obj.base = element.data
        } else if (element.category == 2) {
          obj.hole.push(element.data)
        } else if (element.category == 2) {
          obj.room.push(element.data)
        }
      });
    }
    arr.push(obj)
  });
  res.json(arr)
})
//GET api/map 获取数据

// 获取打洞数据
router.get('/hole', async (req, res) => {
  Hole.find().then(item => {
    res.json(item)
  }).catch(() => {
    res.json("服务器错误")
  })
})
// 新建楼层数据 关联楼层ID
router.post('/hole', async (req, res) => {
  const pro = {};
  if (req.body.title) pro.title = req.body.title;
  if (req.body.data) pro.data = req.body.data;
  if (req.body.floor_id) pro.floor_id = req.body.floor_id;
  if (req.body.floor) pro.floor = req.body.floor;
  if (req.body.category) pro.category = req.body.category;
  if (req.body.color) pro.color = req.body.color;
  if (req.body.heigth) pro.heigth = req.body.heigth;

  new Hole(pro).save().then(item => {
    res.json(item)
    console.log(item);
  })
})
// 修改楼层数据
router.post('/hole/:id', async (req, res) => {
  const pro = {};
  if (req.body.title) pro.title = req.body.title;
  if (req.body.data) pro.data = req.body.data;
  if (req.body.floor_id) pro.floor_id = req.body.floor_id;
  if (req.body.floor) pro.floor = req.body.floor;
  if (req.body.category) pro.category = req.body.category;
  if (req.body.color) pro.color = req.body.color;
  if (req.body.heigth) pro.heigth = req.body.heigth;

  Hole.findOneAndUpdate({ _id: req.params.id }, { $set: pro }, { new: true }).then(item => {
    res.json(item)
  })
})
//DELETE api/profil/delete 删除某一条数据
router.delete('/hole/:id', (req, res) => {
  Hole.findOneAndDelete({ _id: req.params.id }, function (err, Profile) {
    if (!err) {
      console.log("删除成功");
      res.status(200).json('ok')

    }
    console.log(err);

  })
})

// 删除楼层
router.delete('/:id', async (req, res) => {
  Floors.findOneAndDelete({ _id: req.params.id }, function (err, Profile) {
    if (!err) {
      console.log("删除成功");
      res.status(200).json('ok')

    }
    console.log(err);

  })
})
//GET api/map 删除所有数据
router.delete('/', async (req, res) => {
  Map.remove(() => {
    res.json("ok")
  })
})


module.exports = router;
// 5f694e07679f7c03b2b84a26